<?php 

require_once ($DOCUMENT_ROOT . '/includes/functions.php');

session_start ();

if (valid_user ())
{
	$inheader = '<script type="text/javascript" src="/js/bobcat.js"></script><link type="text/css" rel="StyleSheet" href="css/revised.css" />';
	do_header ("Write a Review", $inheader);
	do_menu ();

	if ($submit)
	{
		db_connect ();
			
		$query = "SELECT * FROM ilist WHERE id='$id'";
		$result = mysql_query($query) 
			or report_error ($PHP_SELF, mysql_error (), "can not retrieve teacher", $_SESSION['valid_user']);

		// update average
		$line = mysql_fetch_array($result);		
		$cum = $line[rating] + $rating;
		$total = $line[ratingTotal] + 1;
		
		$query = "UPDATE ilist SET rating='$cum', ratingTotal='$total' WHERE id='$id'";
		$result = mysql_query($query) 
			or report_error ($PHP_SELF, mysql_error (), "can not update teacher", $_SESSION['valid_user']);

		// get user id number
		$tempuser = $_SESSION['valid_user'];
		$query = "SELECT id FROM users WHERE name='$tempuser'";
		$result = mysql_query ($query)
			or report_error ($PHP_SELF, mysql_error (), "could not access user in list", $_SESSION['valid_user']);
		$userid = mysql_result ($result, 0);
		
		// add slashes on to comment
		$comment = addslashes ($comment);

		$sql = "INSERT INTO reviews SET teacherID='$id',
										class='$class',
										term='$term',
										rating='$rating',
										comment='$comment',
										authorID='$userid', 
										date=CURDATE(),
										attendance='$attendance'";
										
		$result = mysql_query($sql)
			or report_error ($PHP_SELF, mysql_error (), "insertion of review failed failed", $_SESSION['valid_user']);
		
		$avg = $cum / $total;
		
		//resolve name from id
		$sql = "SELECT lName, fName FROM ilist WHERE id='$id'";
		$result = mysql_query ($sql)
			or report_error ($PHP_SELF, mysql_error (), "can not resolve name", $_SESSION['valid_user']);
		$row = mysql_fetch_array ($result);
		$fname = $row['fName'];
		$lname = $row['lName'];
		
		echo "<p>Thank you for your comments. $fname $lname's average rating is now $avg</p>\n";
		
		echo "<p><a href='read.php?id=$id'>Read reviews for $fname $lname</a></p>\n";
		echo "<p><a href='list.php'>Back to list of instructors</a></p>\n";
		echo "<p><a href='index.htm'>Go home</a></p>\n";
		
	}
	else
	{
		
		// retrieve name for display purposes
		db_connect ();
		
		$query = "SELECT lName, fName FROM ilist WHERE id='$id'";
		$result = mysql_query ($query);
		$lname = mysql_result ($result, 0);
		$fname = mysql_result ($result, 0, "fName");
?>

<h1>Write a Review for: <?php echo "$fname $lname"; ?></h1>
<form method="post" action="<?php $PHP_SELF ?>" id="form" onSubmit="return verifyReview (this);">
	<table>
      <tr>
        <th scope="col">Term:
          <select size="1" name="term">
          <?php
		$query = 'SELECT * FROM term ORDER BY sequence DESC';	
		$result = mysql_query($query) 
			or report_error ($PHP_SELF, mysql_error (), "term selection failure", $_SESSION['valid_user']);

		// Printing results in HTML
		while ($line = mysql_fetch_array($result))
		{
			if ($line[active] == "1")
			{
				echo "<option value='$line[name]'>$line[name]</option>\n";
			}
		}
	?>
        </select>
          <p>Class:
          <input type="text" name="class" size="20">
          <br />example: Math 182</p>
          <p>Rating:
            <select size="1" name="rating">
              <option value="10">10 (best)</option>
              <option value="9">9</option>
              <option value="8">8</option>
              <option value="7">7</option>
              <option value="6">6</option>
              <option value="5" selected>5</option>
              <option value="4">4</option>
              <option value="3">3</option>
              <option value="2">2</option>
              <option value="1">1 (worst)</option>
            </select>
</p>
        <p>% of class attended:
          <select size="1" name="attendance">
            <option value="10">100%</option>
            <option value="9">90%</option>
            <option value="8">80%</option>
            <option value="7">70%</option>
            <option value="6">60%</option>
            <option value="5">50%</option>
            <option value="4">less</option>
          </select>
</p></th>
        <th scope="col">Review:<br>
<textarea name="comment" id="reviewBox" rows="20" cols="60" onFocus="this.value=''; this.onfocus=null;">
Please write a review to justify your rating.</textarea></th>
      </tr>
    </table>
	<p>Thank you for your time.
	  <input type="submit" value="Submit" name="submit">
	</p>
</form>

<?php
	}
do_footer ();
}
?>

